
package com.iaz.tech.tools.ai.neuron.exec;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.iaz.tech.tools.ai.neuron.runtime.INeuronRuntimeAbility;
import com.iaz.tech.tools.common.utils.UtilTools;
import com.iaz.tech.tools.property.impl.PROPERTYRIGHT;
import com.iaz.tech.tools.property.project.Project;
import com.iaz.tech.tools.uml.IUMLNeuronAble;
import com.iaz.tech.tools.uml.relationship.Relationship;
import com.iaz.tech.tools.uml.structural.classstruct.impl.NeuronEnumStruct;

import lombok.Data;
import lombok.EqualsAndHashCode;

@EqualsAndHashCode(callSuper=false)
@Data
public class NeuronEnumCodeGenerator extends NeuronCodeGenerator {
    /**
     * 日志
     */
	private final static Logger logger = (Logger) LoggerFactory.getLogger(NeuronEnumCodeGenerator.class);

    public NeuronEnumCodeGenerator() {
        super();
    }

    public NeuronEnumCodeGenerator(INeuronRuntimeAbility runtime) {
        super();
        this.setRuntime(runtime);
    }

    @Override
    public Relationship toRelationship(IUMLNeuronAble uml) throws Exception {
        throw new IllegalArgumentException("uml必须为Class");
    }
     /**
     * 生成Class 的对象类
     */
    @Override
    public String generateSourceCode(Project project, IUMLNeuronAble uml) {
        String result = "";
        try {
            if (uml == null) {
                throw new IllegalArgumentException("uml不能为null.");
            }
            if (!(uml instanceof NeuronEnumStruct)){
                throw new IllegalArgumentException("uml必须为NeuronEnumStruct.");
            }
            if (this.getRuntime() == null){
                throw new IllegalArgumentException("runtime不能为null");
            }
            NeuronEnumStruct clazz = (NeuronEnumStruct) uml;
            if (UtilTools.IsNull(clazz.getName())) {
                throw new IllegalArgumentException("Enum名称不能为null");
            }
            StringBuffer sBuf = new StringBuffer();

            if (clazz.getRight() == PROPERTYRIGHT.nprivate) {
                sBuf.append("private ");
            } else if (clazz.getRight() == PROPERTYRIGHT.nprotected) {
                sBuf.append("protected ");
            } else if (clazz.getRight() == PROPERTYRIGHT.npublic) {
                sBuf.append("public ");
            }
            sBuf.append(" enum ");
            sBuf.append(clazz.getName());
            sBuf.append(" {");
            sBuf.append("\n");

            if (clazz.getValues() != null ) {
                int ncount = 0;
                for (String value : clazz.getValues()) {
                    if (ncount > 0) {
                        sBuf.append(",");
                    }
                    sBuf.append(value);                    
                    sBuf.append("\n");
                    ncount++;
                }
            }

            sBuf.append("}");
            result = sBuf.toString();
        } catch(Exception e) {
            logger.error("生成Class代码失败", e);
            result = "";
        }
        return result;
    }

    
}
